package com.xiaojie.controller;


import com.xiaojie.entity.Category;
import com.xiaojie.service.ICategoryService;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.Callable;

/**
 * <p>
 * 分类 前端控制器
 * </p>
 *
 * @author xiaojie
 * @since 2022-07-27
 *
 * selectCategories 自查询
 */
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private ICategoryService categoryService;
    @GetMapping("/categories")
    public List<Category> selectCategories(){
       return categoryService.selectCategories();
    }
    @PatchMapping("/categories/{id}")
    private Category updateCategory(@PathVariable("id") int id,@RequestBody Category category){
        category.setId(id);
        //把系统当前时间作为该属性的值
        category.setUpdatedAt(LocalDateTime.now());
        categoryService.updateById(category);
         return category;
    }
    @DeleteMapping("/categories/{id}")
    public void deleteCategory(@PathVariable("id") int id){
        try{
            categoryService.removeById(id);
        }catch (Exception e){
            throw new RuntimeException("删除失败");
        }
    }
    @PostMapping("/categories")
    public Category add(@RequestBody Category category){
        //修改当前时间
        category.setCreatedAt(LocalDateTime.now());
        category.setUpdatedAt(LocalDateTime.now());
        categoryService.save(category);
        return category;
    }
}

